package controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.EmailModel;
import model.RoomModel;
import service.EmailService;
import service.RoomService;
import service.impl.EmailServiceImpl;
import service.impl.RoomServiceImpl;

/**
 * Servlet implementation class EmailServlet
 */
@WebServlet("/EmailServlet/*")
public class EmailServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private EmailService emailservice=new EmailServiceImpl();   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EmailServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String path=request.getPathInfo().substring(1);
		if("addEmail".equals(path)){
			addEmail(request,response);
		}
	}
	
	private void addEmail(HttpServletRequest request, HttpServletResponse response) {
		
		try {
			EmailModel emailmodel=new EmailModel();
			emailmodel.setUsername((String) request.getSession().getAttribute("username"));
			emailmodel.setMessage((String) request.getParameter("message"));
			String username=emailmodel.getUsername();
			String message=emailmodel.getMessage();
			System.out.println(message);
			emailservice.addEmail(username,message);
			request.getRequestDispatcher("/userlist.jsp").forward(request,response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
